package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.os.Bundle;
import android.view.View;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class QuestionItem extends BindingListItem {
    private boolean started;
    private int id;
    private int uid;
    private String nickname;
    private String avatar;
    private String job;
    private String label;
    private String desc;
    private double money;
    private String title;
    private Date startdate;
    private int browser;
    private int answercoun;
    private int lefttime;
    private String systime;
    private String innerSystime;
    private ListBindingAdapter users;
    private Timer timer;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Bindable
    public String getSystime() {
        return systime;
    }

    public void setSystime(String systime) {
        this.systime = systime;
        notifyPropertyChanged(BR.systime);
    }

    @Bindable
    public String getInnerSystime() {
        return innerSystime;
    }

    public void setInnerSystime(String innerSystime) {
        this.innerSystime = innerSystime;
        notifyPropertyChanged(BR.innerSystime);
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }

    @Bindable
    public Date getStartdate() {
        return startdate;
    }

    public void setStartdate(Date startdate) {
        this.startdate = startdate;
        notifyPropertyChanged(BR.startdate);
    }

    @Bindable
    public int getAnswercoun() {
        return answercoun;
    }

    public void setAnswercoun(int answercoun) {
        this.answercoun = answercoun;
        notifyPropertyChanged(BR.answercoun);
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @Bindable
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
        notifyPropertyChanged(BR.nickname);
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
        notifyPropertyChanged(BR.avatar);
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
        notifyPropertyChanged(BR.job);
    }

    @Bindable
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
        notifyPropertyChanged(BR.label);
    }

    @Bindable
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
        notifyPropertyChanged(BR.desc);
    }

    @Bindable
    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
        notifyPropertyChanged(BR.money);
    }

    @Bindable
    public int getLefttime() {
        return lefttime;
    }

    public void setLefttime(int lefttime) {
        this.lefttime = lefttime;
        notifyPropertyChanged(BR.lefttime);
    }

    @Bindable
    public int getBrowser() {
        return browser;
    }

    public void setBrowser(int browser) {
        this.browser = browser;
        notifyPropertyChanged(BR.browser);
    }

    @Bindable
    public ListBindingAdapter getUsers() {
        return users;
    }

    public QuestionItem(Context context, int layoutId) {
        super(context, layoutId);
        users = new ListBindingAdapter(getContext(), 0);
    }

    public void onClickAvatar(View view) {
        HomeActivity.showPersonalCentral(getContext(), uid);
    }

    /**
     * 点击打开悬赏详情
     *
     * @param view
     */
    public void onClickItem(View view) {
        RewardDetail detail = new RewardDetail(getContext());
        detail.setQuestion(this);

        CommonFragment fragment = new CommonFragment();
        fragment.setVariableId(BR.detail);
        fragment.setDataModel(detail);
        fragment.setLayoutId(R.layout.f_xuanshang_detail);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    public void startTimer() {
        if (started) {
            return;
        }
        timer = new Timer();
        started = true;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (QuestionItem.this.lefttime > 0) {
                    QuestionItem.this.lefttime--;
                    notifyPropertyChanged(BR.lefttime);
                } else {
                    timer.cancel();
                    started = false;
                    timer = null;
                }
            }
        }, 1000, 1000);
    }
}
